package com.tarena.lbs.content.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.tarena.lbs.content.dao.mapper.ArticleMapper;
import com.tarena.lbs.content.dao.mapper.CategoryMapper;
import com.tarena.lbs.content.dao.repository.ArticleRepository;
import com.tarena.lbs.content.pojo.po.ArticleCategoryPO;
import com.tarena.lbs.content.pojo.po.ArticlePO;
import com.tarena.lbs.content.pojo.query.ArticleQuery;
import com.tarena.lbs.content.pojo.vo.ArticleVO;
import com.tarena.lbs.content.pojo.vo.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class ArticleRepositoryImpl implements ArticleRepository {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<ArticleVO> articleList(ArticleQuery articleQuery) {
        PageHelper.startPage(articleQuery.getPageNo(), articleQuery.getPageSize());
        List<ArticleVO> articleVOS = articleMapper.selectArticleList(articleQuery);
        setArticleCategoryInfo(articleVOS);
        return articleVOS;
    }

    /**
     * 根据地理位置查询文章
     */
    @Override
    public List<ArticleVO> articleListByGeo(ArticleQuery articleQuery, List<Integer> articleIds) {
        PageHelper.startPage(articleQuery.getPageNo(), articleQuery.getPageSize());
        List<ArticleVO> articleVOS = articleMapper.selectArticleListByGeo(articleQuery, articleIds);
        setArticleCategoryInfo(articleVOS);
        return articleVOS;
    }

    private void setArticleCategoryInfo(List<ArticleVO> articleVOS) {
        for (ArticleVO articleVO : articleVOS) {
            Integer articleCategoryId = articleVO.getArticleCategoryId();
            if (articleCategoryId != null) {
                ArticleCategoryPO articleCategoryPO = categoryMapper.selectById(articleCategoryId);
                if (articleCategoryPO != null) {
                    articleVO.setArticleCategory(articleCategoryPO.getCategoryName());
                }
            }
        }
    }
//    @Override
//    public List<ArticleVO> articleList(ArticleQuery articleQuery) {
//        PageHelper.startPage(articleQuery.getPageNo(), articleQuery.getPageSize());
//        List<ArticleVO> articleVOS = articleMapper.selectArticleList(articleQuery);
//        for (ArticleVO articleVO : articleVOS) {
//            Integer articleCategoryId = articleVO.getArticleCategoryId();
//            Object articleCategoryPO = categoryMapper.selectById(articleCategoryId);
//            if (articleCategoryPO != null) {
//                articleVO.setArticleCategory(((ArticleCategoryPO) articleCategoryPO).getCategoryName());
//            }
//        }
//        return articleVOS;
//    }

    @Override
    public void updateArticle(ArticlePO param) {
        articleMapper.updateById(param);
    }

    @Override
    public ArticleVO articleDetail(String id) {
        ArticlePO articlePO = articleMapper.selectById(id);
        ArticleVO articleVO = new ArticleVO();
        BeanUtils.copyProperties(articlePO,articleVO);
        return articleVO;
    }

    @Override
    public void insertArticle(ArticlePO articlePO) {
        articleMapper.insert(articlePO);
    }

    @Override
    public void behavior(Integer id, Integer behavior) {
        UpdateWrapper<ArticlePO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        // 设置更新内容：like_count = like_count + 1
        updateWrapper.setSql("like_count = like_count + 1");
        articleMapper.update(updateWrapper);
    }

    @Override
    public List<ArticlePO> selectByShopId(Integer integer) {
        QueryWrapper<ArticlePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id", integer);
        return articleMapper.selectList(queryWrapper);
    }


    @Override
    public Integer findArticles(Integer shopId) {
        Integer count =articleMapper.findArticles(shopId);
        return count;
    }

// 根据文章id查询商家信息
    @Override
    public BusinessVO findBusinessById(Integer id) {
        BusinessVO businessVO = articleMapper.findBusinessById(id);
        return businessVO;
    }
}
